<?php
class Admin_UserController extends SedController 
{
    	public function signupAction(){
    		$smarty = FRM::load('smarty');
    		FRM_XOAD::register('server', $this);
    		
    		$customer = new Customer();
    		
            if (FRM_Request::get('act'))
			{
				$this->loadParams($customer);
	            
				if ($customer->validateInput())
				{
					if(Customer::sendMailSignUp($customer)){
						$customer->create();
						//check user login
						$error_code = Customer::performLogin($customer->username, $customer->confirm_password);
						 
						FRM::redirect(APPLICATION_URL.'/user/garage');
					}
				}
				else{
					$smarty->assign('error', ErrorHandler::getError());
				}			
			}
			$smarty->assign('customer', $customer);
            $smarty->display('public/step1.html');
        }
        
        private function loadParams(&$customer)
        {
        	$customer->username = trim(FRM_Request::getString('username'));
        	$customer->email = trim(FRM_Request::getString('email'));
        	$customer->lastname = trim(FRM_Request::getString('lastname'));
        	$customer->firstname = trim(FRM_Request::getString('firstname'));
        	$customer->telephone = FRM_Request::getString('telephone');
        	$customer->mobilephone = FRM_Request::getString('mobilephone');
        	$customer->address = trim(FRM_Request::getString('address'));
        	$customer->sex = FRM_Request::getIntNumber('sex');
        	$customer->date_create = date('Y-m-d H:i:s');
        	$customer->postcode = trim(FRM_Request::getString('postcode'));
        	$customer->place = trim(FRM_Request::getString('place'));
        	
        	if (!$customer->id)
        	{
        		$customer->password = trim(FRM_Request::getString('password'));
        		$customer->confirm_password = trim(FRM_Request::getString('confirm_password'));
        	}
        
        }
        
        public function checkUserExist($username)
        {
        	return Customer::checkUserExist($username);
        }
        
        public function checkEmailExist($email)
        {
        	return Customer::checkEmailExist($email);
        }
        
        public function loginAction()
        {
        	$smarty = FRM::load('smarty');
        	$error_code = '';
        	if (FRM_Request::isParamSet("username"))
        	{
        		//check user login
        		$error_code = Customer::performLogin($_POST["username"], $_POST["password"]);
        	}
        
        	if (!FRM_Session::exist('user_serialize'))
        	{
        		$smarty->assign("username", (FRM_Request::isParamSet('username') ? FRM_Request::get('username') : ""));
        		$smarty->assign('error', $error_code);
        		$smarty->display('public/login.html');
        	}
        	else
        	{
        		//login successful
        		$cur_login_user = unserialize(FRM_Session::get('user_serialize'));
        		FRM::redirect(APPLICATION_URL);
        	}
        }

        public function logoutAction()
        {
        	// check whether or not the current login user was Admin and it was logged by Login OneClick
        	FRM_Session::remove();
        
        	FRM::redirect(APPLICATION_URL);
        }
        
        function forgotpassAction()
        {
        	$smarty = FRM::load('smarty');
        	$customer = new Customer();
        
        	$result = 0;
        
        	if (FRM_Request::get('act'))
        	{
        		$email = FRM_Request::getString('email');
        		if (Customer::changeForgotPassword($email)){
        			 
        			$result = 1;
        		}
        		else{
        			$result = -1;
        		}
        	}
        	$smarty->assign('result', $result);
        	$smarty->assign('customer', $customer);
        	$smarty->display('public/forgotpass.html');
        }
        
}
?>